<?php

	namespace Emoney\EpayBundle\Manager;

	use Doctrine\ORM\EntityManager;
	use Emoney\EpayBundle\Manager\BaseManager;
	use Emoney\EpayBundle\Entity\StorePayment;
	 
	class StorePayementManager extends BaseManager
	{
		protected $em;
		 
		public function __construct(EntityManager $em)
		{
			$this->em = $em;
		}

		public function loadStorePayement($storePayementId) {
			return $this->getRepository()->findOneBy(array('id' => $storePayementId));
		}
		 
		/**
		* Save store entity
		*
		* @param Desk $desk
		*/
		public function saveDesk(StorePayement $storePayement)
		{
			$this->persistAndFlush($storePayement);
		}
		 
		public function getPayementBySite($siteId) {
			return $this->getRepository()
			->getPayments($siteId);
		}
		
		public function getPreviousDesk($deskId) {
			return $this->getRepository()
			->getAdjacentDesk($deskId, false)
			->getQuery()
			->setMaxResults(1)
			->getOneOrNullResult();
		}

		public function getNextDesk($deskId) {
			return $this->getRepository()
			->getAdjacentDesk($deskId, true)
			->getQuery()
			->setMaxResults(1)
			->getOneOrNullResult();
		}
		 
		public function isAuthorized(Desk $desk, $memberId)
		{
			return ($desk->getMember()->getId() == $memberId) ?	true:false;
		}
		 
		public function getPreviousAndNextDesk($desk)
		{
			return array(
			'prev' => $this->getPreviousDesk($desk->getId()),
			'desk' => $desk,
			'next' => $this->getNextDesk($desk->getId()),
			'voted' => false
			);
		}
		 
		public function getRepository()
		{
			return $this->em->getRepository('EmoneyEpayBundle:StorePayement');
		}

}